{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "pd.set_option('max_columns', 100)\n",
    "import numpy as np \n",
    "import matplotlib.pyplot as plt \n",
    "import seaborn as sns "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [],
   "source": [
    "train = pd.read_csv('happiness_train_complete.csv', encoding='ISO-8859-1')\n",
    "test = pd.read_csv('happiness_test_complete.csv', encoding='ISO-8859-1')\n",
    "IDtest = test['id']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 合并训练集和测试机"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [],
   "source": [
    "train = train[train.happiness != -8]\n",
    "train_len = len(train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\Voyager\\Anaconda3\\lib\\site-packages\\ipykernel_launcher.py:1: FutureWarning: Sorting because non-concatenation axis is not aligned. A future version\n",
      "of pandas will change to not sort by default.\n",
      "\n",
      "To accept the future behavior, pass 'sort=True'.\n",
      "\n",
      "To retain the current behavior and silence the warning, pass sort=False\n",
      "\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    }
   ],
   "source": [
    "df = pd.concat(objs=[train,test], axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>birth</th>\n",
       "      <th>car</th>\n",
       "      <th>city</th>\n",
       "      <th>class</th>\n",
       "      <th>class_10_after</th>\n",
       "      <th>class_10_before</th>\n",
       "      <th>class_14</th>\n",
       "      <th>county</th>\n",
       "      <th>daughter</th>\n",
       "      <th>depression</th>\n",
       "      <th>edu</th>\n",
       "      <th>edu_other</th>\n",
       "      <th>edu_status</th>\n",
       "      <th>edu_yr</th>\n",
       "      <th>equity</th>\n",
       "      <th>f_birth</th>\n",
       "      <th>f_edu</th>\n",
       "      <th>f_political</th>\n",
       "      <th>f_work_14</th>\n",
       "      <th>family_income</th>\n",
       "      <th>family_m</th>\n",
       "      <th>family_status</th>\n",
       "      <th>floor_area</th>\n",
       "      <th>gender</th>\n",
       "      <th>happiness</th>\n",
       "      <th>health</th>\n",
       "      <th>health_problem</th>\n",
       "      <th>height_cm</th>\n",
       "      <th>house</th>\n",
       "      <th>hukou</th>\n",
       "      <th>hukou_loc</th>\n",
       "      <th>id</th>\n",
       "      <th>inc_ability</th>\n",
       "      <th>inc_exp</th>\n",
       "      <th>income</th>\n",
       "      <th>insur_1</th>\n",
       "      <th>insur_2</th>\n",
       "      <th>insur_3</th>\n",
       "      <th>insur_4</th>\n",
       "      <th>invest_0</th>\n",
       "      <th>invest_1</th>\n",
       "      <th>invest_2</th>\n",
       "      <th>invest_3</th>\n",
       "      <th>invest_4</th>\n",
       "      <th>invest_5</th>\n",
       "      <th>invest_6</th>\n",
       "      <th>invest_7</th>\n",
       "      <th>invest_8</th>\n",
       "      <th>invest_other</th>\n",
       "      <th>join_party</th>\n",
       "      <th>...</th>\n",
       "      <th>province</th>\n",
       "      <th>public_service_1</th>\n",
       "      <th>public_service_2</th>\n",
       "      <th>public_service_3</th>\n",
       "      <th>public_service_4</th>\n",
       "      <th>public_service_5</th>\n",
       "      <th>public_service_6</th>\n",
       "      <th>public_service_7</th>\n",
       "      <th>public_service_8</th>\n",
       "      <th>public_service_9</th>\n",
       "      <th>relax</th>\n",
       "      <th>religion</th>\n",
       "      <th>religion_freq</th>\n",
       "      <th>s_birth</th>\n",
       "      <th>s_edu</th>\n",
       "      <th>s_hukou</th>\n",
       "      <th>s_income</th>\n",
       "      <th>s_political</th>\n",
       "      <th>s_work_exper</th>\n",
       "      <th>s_work_status</th>\n",
       "      <th>s_work_type</th>\n",
       "      <th>socia_outing</th>\n",
       "      <th>social_friend</th>\n",
       "      <th>social_neighbor</th>\n",
       "      <th>socialize</th>\n",
       "      <th>son</th>\n",
       "      <th>status_3_before</th>\n",
       "      <th>status_peer</th>\n",
       "      <th>survey_time</th>\n",
       "      <th>survey_type</th>\n",
       "      <th>trust_1</th>\n",
       "      <th>trust_10</th>\n",
       "      <th>trust_11</th>\n",
       "      <th>trust_12</th>\n",
       "      <th>trust_13</th>\n",
       "      <th>trust_2</th>\n",
       "      <th>trust_3</th>\n",
       "      <th>trust_4</th>\n",
       "      <th>trust_5</th>\n",
       "      <th>trust_6</th>\n",
       "      <th>trust_7</th>\n",
       "      <th>trust_8</th>\n",
       "      <th>trust_9</th>\n",
       "      <th>view</th>\n",
       "      <th>weight_jin</th>\n",
       "      <th>work_exper</th>\n",
       "      <th>work_manage</th>\n",
       "      <th>work_status</th>\n",
       "      <th>work_type</th>\n",
       "      <th>work_yr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1959</td>\n",
       "      <td>2</td>\n",
       "      <td>32</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>59</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>11</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>-2.0</td>\n",
       "      <td>3</td>\n",
       "      <td>-2</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>60000.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>45.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>176</td>\n",
       "      <td>1</td>\n",
       "      <td>5</td>\n",
       "      <td>2.0</td>\n",
       "      <td>1</td>\n",
       "      <td>3</td>\n",
       "      <td>50000.0</td>\n",
       "      <td>20000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>12</td>\n",
       "      <td>50</td>\n",
       "      <td>60.0</td>\n",
       "      <td>50</td>\n",
       "      <td>50</td>\n",
       "      <td>30.0</td>\n",
       "      <td>30</td>\n",
       "      <td>50</td>\n",
       "      <td>50</td>\n",
       "      <td>50</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1958.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>40000.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>5.0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>2</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>2</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>2015/8/4 14:18</td>\n",
       "      <td>1</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>-8</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>-8</td>\n",
       "      <td>-8</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>155</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>30.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1992</td>\n",
       "      <td>2</td>\n",
       "      <td>52</td>\n",
       "      <td>6</td>\n",
       "      <td>8</td>\n",
       "      <td>4</td>\n",
       "      <td>5</td>\n",
       "      <td>85</td>\n",
       "      <td>0</td>\n",
       "      <td>3</td>\n",
       "      <td>12</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4.0</td>\n",
       "      <td>2013.0</td>\n",
       "      <td>3</td>\n",
       "      <td>1972</td>\n",
       "      <td>3</td>\n",
       "      <td>1</td>\n",
       "      <td>2</td>\n",
       "      <td>40000.0</td>\n",
       "      <td>3</td>\n",
       "      <td>4</td>\n",
       "      <td>110.0</td>\n",
       "      <td>1</td>\n",
       "      <td>4.0</td>\n",
       "      <td>5</td>\n",
       "      <td>4</td>\n",
       "      <td>170</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2</td>\n",
       "      <td>2</td>\n",
       "      <td>50000.0</td>\n",
       "      <td>20000</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>18</td>\n",
       "      <td>90</td>\n",
       "      <td>70.0</td>\n",
       "      <td>70</td>\n",
       "      <td>80</td>\n",
       "      <td>85.0</td>\n",
       "      <td>70</td>\n",
       "      <td>90</td>\n",
       "      <td>60</td>\n",
       "      <td>60</td>\n",
       "      <td>4</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1</td>\n",
       "      <td>2.0</td>\n",
       "      <td>6.0</td>\n",
       "      <td>2</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>1</td>\n",
       "      <td>2015/7/21 15:04</td>\n",
       "      <td>2</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>4</td>\n",
       "      <td>3</td>\n",
       "      <td>5</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>3</td>\n",
       "      <td>2</td>\n",
       "      <td>4</td>\n",
       "      <td>110</td>\n",
       "      <td>1</td>\n",
       "      <td>3.0</td>\n",
       "      <td>3.0</td>\n",
       "      <td>1.0</td>\n",
       "      <td>2.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2 rows × 140 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   birth  car  city  class  class_10_after  class_10_before  class_14  county  \\\n",
       "0   1959    2    32      3               3                3         1      59   \n",
       "1   1992    2    52      6               8                4         5      85   \n",
       "\n",
       "   daughter  depression  edu edu_other  edu_status  edu_yr  equity  f_birth  \\\n",
       "0         0           5   11       NaN         4.0    -2.0       3       -2   \n",
       "1         0           3   12       NaN         4.0  2013.0       3     1972   \n",
       "\n",
       "   f_edu  f_political  f_work_14  family_income  family_m  family_status  \\\n",
       "0      4            4          1        60000.0         2              2   \n",
       "1      3            1          2        40000.0         3              4   \n",
       "\n",
       "   floor_area  gender  happiness  health  health_problem  height_cm  house  \\\n",
       "0        45.0       1        4.0       3               2        176      1   \n",
       "1       110.0       1        4.0       5               4        170      1   \n",
       "\n",
       "   hukou  hukou_loc  id  inc_ability  inc_exp  income  insur_1  insur_2  \\\n",
       "0      5        2.0   1            3  50000.0   20000        1        1   \n",
       "1      1        1.0   2            2  50000.0   20000        1        1   \n",
       "\n",
       "   insur_3  insur_4  invest_0  invest_1  invest_2  invest_3  invest_4  \\\n",
       "0        1        2         0         1         0         0         0   \n",
       "1        1        1         0         1         0         0         0   \n",
       "\n",
       "   invest_5  invest_6  invest_7  invest_8 invest_other  join_party   ...     \\\n",
       "0         0         0         0         0          NaN         NaN   ...      \n",
       "1         0         0         0         0          NaN         NaN   ...      \n",
       "\n",
       "   province  public_service_1  public_service_2  public_service_3  \\\n",
       "0        12                50              60.0                50   \n",
       "1        18                90              70.0                70   \n",
       "\n",
       "   public_service_4  public_service_5  public_service_6  public_service_7  \\\n",
       "0                50              30.0                30                50   \n",
       "1                80              85.0                70                90   \n",
       "\n",
       "   public_service_8  public_service_9  relax  religion  religion_freq  \\\n",
       "0                50                50      4         1              1   \n",
       "1                60                60      4         1              1   \n",
       "\n",
       "   s_birth  s_edu  s_hukou  s_income  s_political  s_work_exper  \\\n",
       "0   1958.0    6.0      5.0   40000.0          1.0           5.0   \n",
       "1      NaN    NaN      NaN       NaN          NaN           NaN   \n",
       "\n",
       "   s_work_status  s_work_type  socia_outing  social_friend  social_neighbor  \\\n",
       "0            NaN          NaN             2            3.0              3.0   \n",
       "1            NaN          NaN             1            2.0              6.0   \n",
       "\n",
       "   socialize  son  status_3_before  status_peer      survey_time  survey_type  \\\n",
       "0          2    1                2            3   2015/8/4 14:18            1   \n",
       "1          2    0                1            1  2015/7/21 15:04            2   \n",
       "\n",
       "   trust_1  trust_10  trust_11  trust_12  trust_13  trust_2  trust_3  trust_4  \\\n",
       "0        4         3        -8         4         1        2       -8       -8   \n",
       "1        5         3         3         3         2        4        4        3   \n",
       "\n",
       "   trust_5 trust_6  trust_7  trust_8  trust_9  view  weight_jin  work_exper  \\\n",
       "0        5       3        2        3        4     4         155           1   \n",
       "1        5       3        3        3        2     4         110           1   \n",
       "\n",
       "   work_manage  work_status  work_type  work_yr  \n",
       "0          2.0          3.0        1.0     30.0  \n",
       "1          3.0          3.0        1.0      2.0  \n",
       "\n",
       "[2 rows x 140 columns]"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head(2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 建模"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [],
   "source": [
    "train = df[:train_len]\n",
    "test = df[train_len:]\n",
    "# test.drop('city', axis=1, inplace=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {},
   "outputs": [],
   "source": [
    "Y_train = train['happiness'] \n",
    "# X_train = train.drop('happiness', axis=1)\n",
    "predict = ['car', 'class', 'depression', 'gender', 'health', 'house']\n",
    "X_train = train[predict]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 3,  6,  5,  1,  8,  2,  4,  7, 10, -8,  9], dtype=int64)"
      ]
     },
     "execution_count": 129,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['class'].unique()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 130,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Fitting 5 folds for each of 3 candidates, totalling 15 fits\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[Parallel(n_jobs=-1)]: Done  15 out of  15 | elapsed:    5.5s finished\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',\n",
       "            max_depth=None, max_features=0.33, max_leaf_nodes=None,\n",
       "            min_impurity_decrease=0.0, min_impurity_split=None,\n",
       "            min_samples_leaf=1, min_samples_split=2,\n",
       "            min_weight_fraction_leaf=0.0, n_estimators=10, n_jobs=1,\n",
       "            oob_score=False, random_state=None, verbose=0,\n",
       "            warm_start=False)"
      ]
     },
     "execution_count": 130,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "\n",
    "kfold = StratifiedKFold(n_splits=5)\n",
    "rf = RandomForestClassifier()\n",
    "rf_params = {\n",
    "    'max_features': ['auto', 'sqrt', 0.33],\n",
    "    'n_estimators':[10]\n",
    "}\n",
    "gsrf = GridSearchCV(rf, param_grid=rf_params, cv=kfold, scoring='accuracy', n_jobs=-1, verbose=1)\n",
    "gsrf.fit(X_train, Y_train)\n",
    "gsrf.best_estimator_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.5945167751627441"
      ]
     },
     "execution_count": 131,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "gsrf.best_score_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 171,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_happiness = pd.Series(gsrf.predict(test), name='happiness')\n",
    "results = pd.concat([IDtest, test_happiness], axis=1)\n",
    "results.to_csv('0830_submission.csv', index=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "pd.concat([IDtest, test_happiness], axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
